package com.starblink.login.editpersonalinfo.editprofile.ui

import android.content.Intent
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.login.databinding.ActivityEditPersonalProfileBinding

/**
 * 编辑个人简介
 */
@Route(path = RoutePage.Login.EDIT_PERSONAL_PROFILE)
class EditPersonalProfileActivity :
    BaseTVMActivity<ActivityEditPersonalProfileBinding, EditPersonalProfileVM>() {
    @Autowired(name = LoginManager.KEY_PERSONAL_PROFILE)
    @JvmField
    var personalProfile: String = ""

    override fun initViewBinding(): ActivityEditPersonalProfileBinding =
        ActivityEditPersonalProfileBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewBinding.etInput.addTextChangedListener {
            it?.apply {
                setInputLength(length)
            }
        }
        viewBinding.etInput.setText(personalProfile) // 设置初始值

        viewBinding.btnBottom.click {
            // 用户名OK
            setResult(
                RESULT_OK,
                Intent().putExtra(LoginManager.KEY_PERSONAL_PROFILE, viewBinding.etInput.text.toString().trim())
            )
            finish()
        }
    }

    private fun setInputLength(inputLength: Int) {
        viewBinding.tvInputLength.text = "$inputLength/100"
    }
}